package cn.neusoft.neusoft_fz.controller;


import cn.neusoft.neusoft_fz.common.ResponseEntity;
import cn.neusoft.neusoft_fz.service.ReceiptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Receipt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-06-25
 */
@Slf4j
@RestController
@RequestMapping("/receipt")
public class ReceiptController {
    @Autowired
    ReceiptService receiptService;

    @PostMapping("/add/{deliveryOrderId}")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<String> add(@PathVariable Long deliveryOrderId){
        LambdaQueryWrapper<Receipt> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Receipt::getDeliveryOrderId,deliveryOrderId);
        List<Receipt> list = receiptService.list(queryWrapper);
        if (!list.isEmpty()){
            return ResponseEntity.error("无需重复录入");
        }
        Receipt receipt = new Receipt();;
        receipt.setDeliveryOrderId(deliveryOrderId);
        receipt.setReceiptStatus("已收到");
        receiptService.save(receipt);
        ResponseEntity<String> success = ResponseEntity.success("成功录入");
        success.setMsg("成功录入");
        return success;
    }


}
